package com.yc.exception;

import java.util.HashMap;
import java.util.Map;

/**
 * Title: 测试return 和finally执行<br>
 * Description: <br>
 * Author:陈圆(chenyuan@bosssoft.com.cn) <br>
 * Date:2020/9/9
 */
public class TestReturnFinally {

    public static void main(String[] args) {
//        TestReturnFinally testReturnFinally = new TestReturnFinally();
//        System.out.println(testReturnFinally.test1());

        // 以下demo证明 hashMap 的无序性，，主要是在进行hash计算的时候产生了位置交换
        Map<Integer, Integer> map1 = new HashMap<>();
        map1.put(1,1);
        map1.put(2,2);
        map1.put(3,3);
        map1.put(4,4);
        map1.put(5,5);
        map1.put(6,6);
        map1.put(16,6);
        map1.put(32,6);
        map1.put(33,6);
        map1.put(65536,65536);
//        for (int i=0; i<1000; i++) {
//            map1.put(i, i);
//        }
        map1.forEach((k, v) -> System.out.println(k));

    }


//    public int test1() {
//        try {
//            return 2 / 0;
//        } catch (Exception e) {
//            System.out.println("catch了异常");
//            return 1;
//        } finally {
//            System.out.println("执行了finally");
//            return 2;
//        }
//    }

    /**
     * bytecode:
     * Code:
     *        0: iconst_1
     *        1: istore_1
     *        2: iload_1
     *        3: istore_2
     *        4: iconst_3
     *        5: istore_1
     *        6: iload_2
     *        7: ireturn
     *        8: astore_2
     *        9: iconst_2
     *       10: istore_1
     *       11: iload_1
     *       12: istore_3
     *       13: iconst_3
     *       14: istore_1
     *       15: iload_3
     *       16: ireturn
     *       17: astore        4
     *       19: iconst_3
     *       20: istore_1
     *       21: aload         4
     *       23: athrow
     */
    public int test2() {
        int x;
        try {
            x = 1;
            return x;
        } catch (Exception e) {
            x = 2;
            return x;
        } finally {
            x = 3;
        }
    }
}
